package fr.generali.accueilclient.gwt.client.ui.components.link;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class LabelLink extends Composite implements HasClickHandlers {

    private static LabelLinkUiBinder uiBinder = GWT.create(LabelLinkUiBinder.class);

    interface LabelLinkUiBinder extends UiBinder<Widget, LabelLink> {
    }

    public interface Resources extends ClientBundle {

        @Source({"components/link/labelLink.css" })
        Style style();

        interface Style extends CssResource {

            String link();

            String image();

        }
    }

    @UiField
    Resources res;

    @UiField
    Label label;

    @UiField
    Image image;

    public LabelLink() {
        initWidget(uiBinder.createAndBindUi(this));
        res.style().ensureInjected();
    }

    public LabelLink(String text) {
        this();
        setText(text);
    }

    public LabelLink(ImageResource imageResource) {
        this();
        setImageResource(imageResource);
    }

    public void setText(String text) {
        label.setText(text);
    }

    public String getText() {
        return label.getText();
    }

    @Override
    public HandlerRegistration addClickHandler(ClickHandler handler) {
        
        final HandlerRegistration imgReg = image.addClickHandler(handler);
        
        final HandlerRegistration labelReg = label.addClickHandler(handler);
        
        HandlerRegistration registration = new HandlerRegistration() {
            
            @Override
            public void removeHandler() {
                imgReg.removeHandler();
                labelReg.removeHandler();
                
            }
        };
        
       
        return registration;
    }

    public void setImageResource(ImageResource resource) {
        image.setResource(resource);
        image.addStyleName(res.style().image());
    }

}
